#!/bin/bash
if(test -L $0) then
    	auxlink=`ls -l $0 | sed 's/^[^>]*-> //g'`
    	M_HOME=`dirname $auxlink`/
else 
    	M_HOME=`dirname $0`/
fi
cd $M_HOME

PACKAGE_DIR=$PWD/package
PATCH_DIR=$PWD/patch
RESULT_DIR=/opt/armgcc
export BUILD_DIR=$PWD/build
export STATIC_LIB_DIR=$RESULT_DIR/staticlibs
export ARM_ROOT_DIR=$RESULT_DIR/armroot
export SOURCE_DIR=$PWD/source
export TARGET=arm-linux-gnueabi
export HOST=`gcc -dumpmachine`

# check directory exists
[ -d $PACKAGE_DIR -a -d $BUILD_DIR -a -d $PATCH_DIR -a -d $SOURCE_DIR -a -d $RESULT_DIR ]
if [ $? -ne 0 ] ; then
	echo 'one of the following does not exist: '
	echo $PACKAGE_DIR
	echo $BUILD_DIR
	echo $PATCH_DIR
	echo $SOURCE_DIR
	echo $RESULT_DIR
	exit 1
fi

if [ -f pkglist -o -f patchlist -o -f varlist ]; then
	echo '`pkglist`,`patchlist` or `varlist` exist, I will not overwrite them. Abort.'
	exit 1
fi

$PACKAGE_DIR/download pkglist || exit 1
$PATCH_DIR/download patchlist || exit 1
$SOURCE_DIR/unpackAndPatch pkglist patchlist || exit 1
./dirvar pkglist > varlist || exit 1
rm pkglist patchlist -f

unset CFLAGS CXXFLAGS
if [ `uname -m` == x86_64 ]; then export LIB_CFLAGS="-fPIC"; fi

./installkernelheader || exit 1
./installstaticlibs || exit 1
./installbinutils || exit 1
./installgcc1 || exit 1
./installglibc || exit 1
./installgcc2 || exit 1
./installlink || exit 1

rm varlist -f
